package com.chen.controller;

import com.chen.domain.User;
import com.chen.dto.R;
import com.chen.model.GoogleVo;
import com.chen.service.UserService;
import com.chen.utils.GoogleAuthenticator;
import com.chen.utils.UserUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Date: 2021/4/29 11:16 AM
 * @Version 1.0
 */
@RestController
@RequestMapping("/user/ga")
public class GoogleController {

    @Autowired
    private UserService userService;

    @GetMapping("/generate")
    @ApiOperation(value = "获取谷歌验证码")
    public R generate() {
        Long userId = UserUtil.getUser();
        User byId = userService.getById(userId);
        String secret = GoogleAuthenticator.generateSecretKey();
        // 把这个qrcode生成二维码，用google身份验证器扫描二维码就能添加成功
        String qrcode = GoogleAuthenticator.getQRBarcode(byId.getMobile() + "[coin-exchange]", secret);
        Map<String, Object> result = new HashMap<>();
        result.put("otpAuthTotpURL", qrcode);
        result.put("secret_key", secret);
        System.out.println("qrcode:" + qrcode + ",key:" + secret);
        return R.ok(result);
    }


    @PostMapping("/verify")
    @ApiOperation(value = "验证谷歌验证码")
    public R verify(@RequestBody GoogleVo googleVo) {
        userService.gaVerify(googleVo);
        return R.ok();

    }

    @PostMapping("/cancel")
    @ApiOperation(value = "关闭谷歌验证码")
    public R cancel(@RequestBody GoogleVo googleVo) {
        userService.gaCancel(googleVo);
        return R.ok();

    }

}
